#include "SelectButton.h"

SelectButton* SelectButton::createButton(const char *buttonName, Vec2 pos, Node *scene) {
	SelectButton *button = new SelectButton();
	if (button && button->initButton(buttonName, pos, scene)) {
		button->autorelease();
		return button;
	}
	CC_SAFE_DELETE(button);
	return nullptr;
}

SelectButton::~SelectButton() {
	CC_SAFE_RELEASE(m_buttonName);
	CC_SAFE_RELEASE(m_texture_up);
	CC_SAFE_RELEASE(m_texture_down);
	CC_SAFE_RELEASE(m_listener);
	CC_SAFE_RELEASE(m_texture_locked);
	CC_SAFE_RELEASE(m_unselectedicon);
	CC_SAFE_RELEASE(m_preSkills);
}

bool SelectButton::initButton(const char *buttonName, Vec2 pos, Node *scene) {
	m_parentNode = scene;
	m_buttonName = __String::create(buttonName);
	this->setPosition(pos);
	m_listener = EventListenerTouchOneByOne::create();
	CC_SAFE_RETAIN(m_listener);
	m_listener->onTouchBegan = CC_CALLBACK_2(SelectButton::onTouchBegan, this);
	m_listener->onTouchMoved = CC_CALLBACK_2(SelectButton::onTouchMoved, this);
	m_listener->onTouchEnded = CC_CALLBACK_2(SelectButton::onTouchEnded, this);

	m_upgrade_enable = true;
	m_isLocked = !USERDEFAULT->getBoolForKey(buttonName);
	m_isSelected = !strcmp(USERDEFAULT->getStringForKey(SELECTEDBOW_KEY).c_str(), buttonName);
	m_texture_up = SpriteFrame::create(__String::createWithFormat("SelectScene/%s_icon.png", buttonName)->getCString(), Rect(0, 0, 78, 78));
	m_texture_down = SpriteFrame::create(__String::createWithFormat("SelectScene/%s_icon_down.png", buttonName)->getCString(), Rect(0, 0, 78, 78));
	m_texture_locked = SpriteFrame::create(LOCKEDICON_NAME, Rect(0, 0, 78, 78));
	m_showSprite = Sprite::create();
	this->addChild(m_showSprite);
	m_unselectedicon = Sprite::create(UNSELECTICON_NAME);
	CC_SAFE_RETAIN(m_buttonName);
	CC_SAFE_RETAIN(m_texture_up);
	CC_SAFE_RETAIN(m_texture_down);
	CC_SAFE_RETAIN(m_texture_locked);
	CC_SAFE_RETAIN(m_unselectedicon);
	if (!m_isLocked) {
		m_showSprite->setSpriteFrame(m_texture_up);
		if (!m_isSelected) {
			this->addChild(m_unselectedicon, 1, "selected");
		}
	}
	else {
		m_showSprite->setSpriteFrame(m_texture_locked);
	}
	this->onStart();
	return true;
}

void SelectButton::setPreSkills(__Array *preSkills) {
	m_preSkills = preSkills;
	CC_SAFE_RETAIN(m_preSkills);
	for (int i = 0; i < m_preSkills->count(); i++) {
		if (!USERDEFAULT->getBoolForKey(dynamic_cast<__String *>(m_preSkills->getObjectAtIndex(i))->getCString())) m_upgrade_enable = false;
	}
}

void SelectButton::onStart() {
	DIRECTOR->getEventDispatcher()->addEventListenerWithSceneGraphPriority(m_listener, this);
}

void SelectButton::onStop() {
	DIRECTOR->getEventDispatcher()->removeEventListener(m_listener);
}

void SelectButton::unLock() {
	if (USERDEFAULT->getBoolForKey(EFFECT_ENABLE_KEY))
		AUDIOENGINE->playEffect(UNLOCKBOW_EFFECT_NAME);
	m_showSprite->setSpriteFrame(m_texture_up);
	m_isLocked = false;
	USERDEFAULT->setBoolForKey(m_buttonName->getCString(), true);
	USERDEFAULT->flush();
	this->onEnter();
}

void SelectButton::changeSelected() {
	if (!m_isSelected) {
		auto scene = dynamic_cast<SelectScene *>(m_parentNode);
		scene->unselectButtons();
		m_isSelected = true;
		this->removeChildByName("selected", false);
		USERDEFAULT->setStringForKey(SELECTEDBOW_KEY, m_buttonName->getCString());
		USERDEFAULT->flush();
	}
}

void SelectButton::setSelected(bool selected) {
	m_isSelected = selected;
	this->addChild(m_unselectedicon, 1, "selected");
}

bool SelectButton::isTouchedInside(Touch *pTouch) {
	Vec2 touchPos = pTouch->getLocation();
	Vec2 nodePos = this->convertToNodeSpace(touchPos);
	if (Rect(-m_showSprite->getContentSize().width * 0.5, -m_showSprite->getContentSize().height * 0.5, m_showSprite->getContentSize().width, m_showSprite->getContentSize().height).containsPoint(nodePos)) {
		return true;
	}
	return false;
}

bool SelectButton::onTouchBegan(Touch *pTouch, Event *event) {
	if (isTouchedInside(pTouch)) {
		m_upgrade_enable = true;
		for (int i = 0; i < m_preSkills->count(); i++) {
			if (!USERDEFAULT->getBoolForKey(dynamic_cast<__String *>(m_preSkills->getObjectAtIndex(i))->getCString())) m_upgrade_enable = false;
		}
		if (!m_isLocked) {
			m_showSprite->setSpriteFrame(m_texture_down);
		}
		return true;
	}
	return false;
}

void SelectButton::onTouchMoved(Touch *pTouch, Event *event) {
	if (!isTouchedInside(pTouch)) {
		if (!m_isLocked) {
			m_showSprite->setSpriteFrame(m_texture_up);
		}
	}
}

void SelectButton::onTouchEnded(Touch *pTouch, Event *event) {
	if (isTouchedInside(pTouch)) {
		if (m_isLocked) {
			if (Player::getInstance()->getJewel() >= 250 && m_upgrade_enable) {
				Player::getInstance()->setJewel(Player::getInstance()->getJewel() - 250);
				this->unLock(); 
				this->changeSelected();
				m_showSprite->setSpriteFrame(m_texture_up);
			}
			else {
				if (USERDEFAULT->getBoolForKey(EFFECT_ENABLE_KEY))
					AUDIOENGINE->playEffect(GOLDOVER_EFFECT_NAME);
			}
		}
		else {
			if (USERDEFAULT->getBoolForKey(EFFECT_ENABLE_KEY))
				AUDIOENGINE->playEffect(CLICKEFFECT_NAME);
			m_showSprite->setSpriteFrame(m_texture_up);
			this->changeSelected();
		}
	}
}